package com.wj.controller;


import com.wj.responese.ResultResponse;
import com.wj.resquest.VerificationCodeDTO;
import com.wj.service.VerificationCodeService;
import com.wj.util.PhoneNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VerificationCodeController {
    @Autowired
    private VerificationCodeService verificationCodeService;

    @GetMapping("/verification-code")
    public ResultResponse<String> verificationCode(@RequestBody VerificationCodeDTO verificationCodeDTO) {
        String passengerPhone = verificationCodeDTO.getPassengerPhone();
        try {
            if (!PhoneNumberUtil.isValidPhoneNumber(passengerPhone)) {
                return ResultResponse.failure("Invalid phone number: Incorrect format");
            }
            return verificationCodeService.sendVerificationCode(passengerPhone);
        } catch (Exception e) {
            // 这里处理可能的异常，例如记录日志或返回一个通用的错误消息
            return ResultResponse.failure("Error occurred while sending verification code");
        }
    }
}
